//
//  CTPhotoBrowser.swift
//  CTPhotoBrowser
//
//  Created by Andney Jack on 2021/2/25.
//

import UIKit

@objc public protocol CTPhotoBrowserDelegate: NSObjectProtocol {
    /// 长按事件
    @objc optional func photoBrowserDidLongPress(_ photoBrowser: CTPhotoBrowser, at itemView: CTPhotoBrowserItemView, index: Int) -> Void
    /// 消失完成的回调
    @objc optional func photoBrowserDidDismiss(_ photoBrowser: CTPhotoBrowser) -> Void
    /// 低清晰度的占位图
    @objc optional func photoBrowser(_ photoBrowser: CTPhotoBrowser, lowQualityImageFor index: Int) -> UIImage?
    /// 加载图片
    @objc optional func photoBrowserLoadImage(_ photoBrowser: CTPhotoBrowser, itemView: CTPhotoBrowserItemView, imageObj: Any) -> Void
}

/// 照片浏览器
public class CTPhotoBrowser: UIViewController {
    private var bannerView: CTBaseCycleScrollView<CTPhotoBrowserItemView>?
    private var indexLabel: UILabel? // 序号显示
    private weak var delegate: CTPhotoBrowserDelegate?
    private var placeholderImage: UIImage?

    /// 标识符
    public var identifier: String?

    public var currentIndex: Int? {
        return bannerView?.currentIndex
    }

    public var imageList: [Any]? {
        return bannerView?.imageObjList
    }

    override public func viewDidLoad() {
        super.viewDidLoad()
        m_loadUI()
    }

    func m_loadUI() {
        bannerView = CTBaseCycleScrollView<CTPhotoBrowserItemView>(frame: view.bounds, itemSpacing: 10, itemBuilder: {
            let itemView = CTPhotoBrowserItemView()
            itemView.singleTapHandler = { [weak self] in
                self?.m_itemViewSingleTap($0)
            }
            itemView.longPressHandler = { [weak self] in
                self?.m_itemViewSingleTap($0)
            }
            return itemView
        })
        bannerView?.autoScroll = false
        bannerView?.imageLoadHandler = { [weak self] itemView, imgObj, _, _ in
            guard let photoBrowser = self else { return }

            CTPhotoBrowser.loadImage(itemView: itemView, imageObj: imgObj, placeholder: photoBrowser.placeholderImage)
        }
        view.addSubview(bannerView!)

        indexLabel = UILabel()
        indexLabel?.backgroundColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 0.4)
        indexLabel?.layer.cornerRadius = 16
        indexLabel?.layer.masksToBounds = true
        indexLabel?.font = UIFont.systemFont(ofSize: 18)
        indexLabel?.textColor = .white
        indexLabel?.numberOfLines = 0
        indexLabel?.textAlignment = .center
        view.addSubview(indexLabel!)

//        WS(weakSelf)
//            [_indexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.centerX.equalTo(weakSelf.view);
//                make.size.mas_equalTo(CGSizeMake(100, 32));
//                make.top.equalTo(weakSelf.view).offset(HEIGHT_STATUS_BAR + 8);
//            }];
    }

    // MARK: - —————— Public

    /// 展示图片浏览器
    /// - Parameters:
    ///   - imageList: 图片对象列表
    ///   - pageVC: 从哪个页面显示
    ///   - currentIndex: 指定要显示图片的索引，默认第一张
    ///   - placeholderImage: 占位图
    ///   - delegate: 代理
    /// - Returns: 是否显示成功
    public class func show(_ imageList: [Any], inPage pageVC: UIViewController, currentIndex: Int = 0, placeholderImage: UIImage?, delegate: CTPhotoBrowserDelegate? = nil) -> Bool {
        guard imageList.count > 0 else {
            return false
        }

        return false
    }

//    public func

    // MARK: - Event

    private func m_itemViewSingleTap(_ itemView: CTPhotoBrowserItemView) {
    }

    private func m_itemViewLongPress(_ itemView: CTPhotoBrowserItemView) {
    }
}
